
(ns model.vector3D
(:refer-clojure :exclude (vector))
)


(defstruct vector3D :x :y :z)

(defn make
  ([]
    (struct vector3D 0 0 0))
  ([x y z]
    (struct vector3D x y z))
  )

(defn add [v1 v2]
  (make
    (+ (:x v1) (:x v2))
    (+ (:y v1) (:y v2))
    (+ (:z v1) (:z v2)))
  )

(defn sub [v1 v2]
  (make
    (- (:x v1) (:x v2))
    (- (:y v1) (:y v2))
    (- (:z v1) (:z v2)))
  )

(defn s-mul [v s]
  (make
    (* (:x v) s)
    (* (:y v) s)
    (* (:z v) s))
  )

(defn norm [v]
  (letfn [(square [x] (* x x))]
    (Math/sqrt
      (+
        (square (:x v))
        (square (:y v))
        (square (:z v))))
    )
  )

(defn unit [v]
  (s-mul v (/ 1 (norm v))))

(defn rotateXY [{x :x y :y z :z}]
  (make (- y) x z))

(defn distance [p q]
  (letfn [(square [x] (* x x))]
    (Math/sqrt
      (+
        (square (- (:x p) (:x q)))
        (square (- (:y p) (:y q)))
        (square (- (:z p) (:z q)))))
    )
  )